package dao;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import parsers.calendar.CalendarsParser;
import parsers.calendar.MainCalendarsParser;

import model.calendar.Calendar;
import model.calendar.MainCalendar;

public class CalendarDAO 
{
	private static Map<String, MainCalendar> mainCalendarsCP = new HashMap<String, MainCalendar>();
	private static Map<String, Calendar> calendarsCP = new HashMap<String, Calendar>();
	
	private static String currentDate = util.Utilities.getDateTime();
	
	private static String[] file = new String[] {makeFileName("macCP"), makeFileName("calCP")};
	
	private long startTime;
	
	@SuppressWarnings("unchecked")
	public CalendarDAO()
	{
		startTime();
		System.out.print("Initialising Calendars: ");
		if( (new File(file[0])).exists() && (new File(file[1])).exists())
		{
			System.out.print(" (find serialized files " + file[0] + "; " + file[1] + ") ");
			mainCalendarsCP = (Map<String, MainCalendar>) util.Utilities.deserialize(file[0]);
			calendarsCP = (Map<String, Calendar>) util.Utilities.deserialize(file[1]);
		}
		else
		{
			initMainCalendar();
		}
		System.out.println(countTime() + " ms");
	}
	
	private static String makeFileName(String name)
	{
		return Linker.path + CalendarDAO.class.getName() + name + currentDate + ".jsr";
	}
	
	public static void serialize()
	{
		if( !(new File(file[0])).exists() )
		{
			util.Utilities.serialize(mainCalendarsCP, file[0]);
		}
		
		if( !(new File(file[1])).exists() )
		{
			util.Utilities.serialize(calendarsCP, file[1]);
		}
	}
	
	private void initMainCalendar()
	{
		MainCalendarsParser mcp = new MainCalendarsParser();
		
		try
		{
			ArrayList<MainCalendar> mainCalendars = mcp.parse(new URL("http://plan.uz.zgora.pl/kalendarze_lista.php"));
			
			for (int i = 0; i < mainCalendars.size(); i++)
			{
				CalendarsParser cp = new CalendarsParser(mainCalendars.get(i));
				MainCalendar mainCalendar = cp.parse(true);
				
				MainCalendar newMainCalendar = new MainCalendar(mainCalendar.getName(), mainCalendar.getDescription(), mainCalendar.getWarning(), mainCalendar.getShared());
				newMainCalendar.setId(mainCalendar.getId());
				newMainCalendar.setLink(mainCalendar.getLink());
				
				ArrayList<Calendar> calendars = mainCalendar.getCalendars();
				
				for (int j = 0; j < calendars.size(); j++)
				{
					calendarsCP.put(calendars.get(j).getId(), calendars.get(j));
					newMainCalendar.createCalendar(calendars.get(j).getId(), calendars.get(j).getName());
				}

				mainCalendarsCP.put(newMainCalendar.getId(), newMainCalendar);
			}
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public static Map<String, MainCalendar> getMainCalendarsCP()
	{
		return mainCalendarsCP;
	}
	
	public static Map<String, Calendar> getCalendarsCP()
	{
		return calendarsCP;
	}
	
	private void startTime()
	{
		startTime = System.currentTimeMillis();
	}
	
	private long countTime()
	{
		return System.currentTimeMillis() - startTime;
	}
}